package example;

import static info.motteke.qjdbc.column.MetaInnerApi.getValue;
import info.motteke.qjdbc.column.IntMeta;
import info.motteke.qjdbc.column.Meta;
import info.motteke.qjdbc.column.StringMeta;
import info.motteke.qjdbc.column.variable.IntVariable;
import info.motteke.qjdbc.column.variable.StringVariable;
import info.motteke.qjdbc.column.variable.Variable;
import info.motteke.qjdbc.db.Database;
import info.motteke.qjdbc.dto.DtoMeta;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

public class SampleMeta extends DtoMeta<Sample> {

    private static final SampleMeta instance = new SampleMeta();

    public final StringMeta name = new StringMeta("name");

    public final IntMeta value = new IntMeta("value");

    public static SampleMeta get(){
        return instance;
    }

    protected SampleMeta() {
        super(Sample.class, "Sample", Database.get("SQLServer"));
    }

    @Override
    protected Sample newInstanceImpl() {
        return new Sample();
    }

    @Override
    protected void setValuesImpl(ResultSet rs, Sample obj, Set<Meta> columns) throws SQLException {
        for(Meta meta : columns){
            if(meta.equals(name)){
                obj.setName(getValue(name, rs));
                break;
            }
            if(meta.equals(value)){
                obj.setValue(getValue(value, rs));
                break;
            }
        }
    }

    @Override
    protected void putValuesImpl(Sample obj, List<Pair> variables) throws SQLException {
        for(Pair pair : variables){
            Meta meta = pair.getColumn();
            Variable var = pair.getVariable();
            if(meta.equals(name)){
                StringVariable v = (StringVariable) var;
                v.is(obj.getName());
                break;
            }
            if(meta.equals(value)){
                IntVariable v = (IntVariable) var;
                v.is(obj.getValue());
                break;
            }
        }
    }
}
